package com.water.controller;

import com.water.entity.SyEmp;
import com.water.entity.SyEmpmenupower;
import com.water.service.SyEmpService;
import com.water.service.SyEmpmenupowerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * (SyEmpmenupower)表控制层
 *
 * @author makejava
 * @since 2020-12-14 19:19:01
 */
@Controller
@RequestMapping("syEmpmenupower")
public class SyEmpmenupowerController {
    /**
     * 服务对象
     */
    @Resource
    private SyEmpmenupowerService syEmpmenupowerService;

    @Resource
    private SyEmpService syEmpService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public SyEmpmenupower selectOne(Integer id) {
        return this.syEmpmenupowerService.queryById(id);
    }

    @ResponseBody
    @RequestMapping(value = "enmuname/{id}",method = RequestMethod.POST)
    public List<SyEmpmenupower> menuname(@RequestBody @PathVariable ("id") Integer id){
  List<SyEmpmenupower> syEmpmenupowerList=syEmpmenupowerService.queryMenu(id);
        System.out.println(syEmpmenupowerList.toArray());
        System.out.println(syEmpmenupowerList.toString());
        return syEmpmenupowerList;
    }


    /**
     * 跳转到菜单权限
     * @param syEmp
     * @param model
     * @return
     */
    @RequestMapping(value = "sys_menupower",method = RequestMethod.GET)
    public String sys_menupower(SyEmp syEmp, Model model){
        syEmp.setDisabled(0);
        List<SyEmp> syempList= this.syEmpService.queryAll(syEmp);
        System.out.println(syempList);
        model.addAttribute("list",syempList);
        return "sys/sys_menupower";
    }


    /**
     * 设置权限
     * @param id
     * @return
     */
    @RequestMapping(value = "setmenu/{id}",method = RequestMethod.GET)
    public String setmenu(@PathVariable("id") String id,Model model){
           List<SyEmpmenupower> syEmpmenupowerList=this.syEmpmenupowerService.queryByListId(id);
        System.out.println(syEmpmenupowerList.toString());
        System.out.println("进入修改菜单权限");
    SyEmp syEmp= (SyEmp) this.syEmpService.queryById(Integer.parseInt(id));
        model.addAttribute("syEmp",syEmp);
        model.addAttribute("syEmpmenu",syEmpmenupowerList);
        return "sys/sys_menupower_set";
    }

    @ResponseBody
    @RequestMapping(value = "/a",method = RequestMethod.GET)
    public String test(){
        return "test";
    }

//    @RequestMapping(value = "")
//    public List<SyEmpmenupower> findQuanList(@PathVariable("id") String id){
//        List<SyEmpmenupower> quanList=this.syEmpmenupowerService.findQuanList(id);
//        return
//    }


    /**
     * 设置权限
     * @return
     */
    @RequestMapping(value = "setmenu1",method = RequestMethod.POST)
    public String setmenu1(String array1, String id){
   int  a=this.syEmpmenupowerService.deleteByEmpId(Integer.parseInt(id));
        System.out.println("删除权限成功"+a);
   List<SyEmpmenupower> syEmpmenupowerList=new ArrayList<>();
          String arr[]=array1.split(",");
        for (int i = 0; i < arr.length; i++) {
            SyEmpmenupower syEmpmenupower=new SyEmpmenupower();
                    syEmpmenupower.setEmpid(Integer.parseInt(id));
                    syEmpmenupower.setMenuid(Integer.parseInt(String.valueOf(arr[i])));
                    syEmpmenupowerList.add(syEmpmenupower);
            System.out.println(arr[i]);
        }
        System.out.println(syEmpmenupowerList+"集合");
      int a1=  this.syEmpmenupowerService.insertBatch(syEmpmenupowerList);
    if(a1!=0){
return "success";
    }
return "error";
    }
}